package com.example.carrierapp.ui.main.find;

import android.content.Context;

import com.example.carrierapp.base.BasePresenter;
import com.example.carrierapp.bean.find.FindGoodsListBean;
import com.example.carrierapp.net.ApiMiddleHelper;
import com.example.carrierapp.net.listener.DataCallBackListener;
import com.example.carrierapp.util.LogUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Flowable;

public class FindGoodsPresenter extends BasePresenter<FindGoodsContract.View> implements FindGoodsContract.Presenter {
    private Context mContext;
    public FindGoodsPresenter(Context context) {
        mContext=context;
    }

    @Override
    public void start() {

    }

    @Override
    public void getFindGoodsList(String loadingAddress, String unloadingAddress, int recentDays,int skipCount) {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("maxResultCount", "10");
        biddingMap.put("recentDays", recentDays+"");
        biddingMap.put("loadingAddress",loadingAddress);
        biddingMap.put("unloadingAddress", unloadingAddress);
        biddingMap.put("skipCount", skipCount + "");
        Flowable<JsonObject> flowable = apiStrores.getFindGoodsList(biddingMap);

        LogUtil.d("getFindGoodsList-" + loadingAddress+"-"+unloadingAddress+"-"+skipCount);
        ApiMiddleHelper.getInstance().toNetIo2Main(mContext,flowable, new DataCallBackListener() {

            @Override
            public void onSuccess(String json) {
                LogUtil.d("getFindGoodsList-onSuccess-" + json);

                FindGoodsListBean bean = new Gson().fromJson(json, new TypeToken<FindGoodsListBean>() {
                }.getType());
                if (getView() != null) {
                    getView().getFindGoodsListSuccess(bean);
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("getFindGoodsList-onFailure-" + msg);
                if (getView() != null) {
                    getView().getFindGoodsListFailde(msg);
                }
            }
        });
    }
}
